//praneet sharma
//2010061
//book referred - let us C by yashwant kanitkar


#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
int m=23;
struct node{
	char user[30];
	char pass[30];
}a[23];

int ascii_check(char p[])
{
	
	int str_len,i;
	
	int asc;
	int sum=0;
	str_len=strlen(p);
	for(i=0;i<=str_len-1;i++)
	{
	                  asc=p[i];
	                  sum=sum+asc;
	}
	
	return sum;
}

int hash_func_1(int k)
{
	int hash1;
	hash1=k%m;
	return hash1;
}

int hash_func_2(int k)
{
	int hash1;
	float b=0.6;
	float c=k*b;
	hash1=m*(c-(int)c);
	
	return hash1;
}

int hash_func_3(char p[30])
{
	int ascii_first;
	ascii_first=p[0];
	int str_len=strlen(p);
	int hash1;
	hash1=(ascii_first*str_len)%m;

	return hash1;

}	

	
int main()
{
	FILE *fp;
	char s[80];
	char p[15];
	char q[15];
	int s1,hash1;
	int choi;
	fp=fopen("userpass_2.txt","r");
	if(fp==NULL)
	{
		puts("cannot open file");
		exit(0);
	}
	char ch;
	
	int m,l;
	int j=0;
	
	int ac=0;
	

	printf("\nlist of hash functions\n----------------------\n");
	printf("1 for hash function 1 function\n");
	printf("2 for hash function 2 function\n");
	printf("3 for hash function 3 function\n");
	printf("enter your choice:");
	scanf("%d",&choi);
	
	ch=fgetc(fp);
	while(ch!=EOF)
	{
		int count=0,k=0,i=0;
		
		
		while(ch!='\n')
		{
		
			s[i]=ch;
			i++;
			count++;
			ch=fgetc(fp);
		}
	
		s[i]='\0';
		
	
		while(s[k]!=' ')
		{
			p[k]=s[k];
			
			k++;
		
		}
		p[k]='\0';
		int temp[80];
	
		
		for(m=0,l=k;l<=count-1;l++,m++)
		{
			q[m]=s[l];
			
		}
		q[m]='\0';
		
	
	
		
		if(choi==1)
		{
			s1=ascii_check(p);
			hash1=hash_func_1(s1);
		}
		else if(choi==2)
		{
			s1=ascii_check(p);
			hash1=hash_func_2(s1);
		}
		else if(choi==3)
		{
			hash1=hash_func_3(p);
		}
		
		
		
	
		
		int h2,h3,hash2,hash3;
		
		
		
		hash3=hash1+1;
		
		if(strcmp(a[hash1].user,"")!=0)
		{
			
			while(strcmp(a[hash3].user,"")!=0)
			{
				hash3++;
			}
			if(strcmp(a[hash3].user,"")==0)
			{
				
				strcpy(a[hash3].user,p);
				strcpy(a[hash3].pass,q);
			}
		}

		else if(strcmp(a[hash1].user,"")==0)
		{
			
			strcpy(a[hash1].user,p);
			strcpy(a[hash1].pass,q);
		}

		
		
	

	ch=fgetc(fp);
	}

	
	


	char check_user[20];
	setbuf(stdin,NULL);
	printf("please, enter your username:");
	gets(check_user);

	char check_pass[20];
	int check_hash1;
	int check_hash3;
	int checker=0;
	int count_checker=0;	
	
	int check_s1;
	int n=23;
	if(choi==1)
	{
		check_s1=ascii_check(check_user);
		
		check_hash1=hash_func_1(check_s1);
	}
	else if(choi==2)
	{
		check_s1=ascii_check(check_user);
		
		check_hash1=hash_func_2(check_s1);
	}

	else if(choi==3)
	{
		
		check_hash1=hash_func_3(check_user);
	}

	
	check_hash3=check_hash1+1;
	
	if(strcmp(a[check_hash1].user,check_user)==0)
	{
			printf("enter your password by first putting a space:");
			gets(check_pass);
			
			if(strcmp(check_pass,a[check_hash1].pass)==0)
			{
				printf("\nyou have successfully logged in\n\n");
				return;
			}
	}

	

	else if(strcmp(a[check_hash1].user,check_user)!=0)
	{
		
		while(strcmp(a[check_hash3].user,check_user)!=0)
		{
			count_checker++;
			
			if(count_checker==n)
			{
				break;
			}
			check_hash3++;
		}
		if(strcmp(a[check_hash3].user,check_user)==0)
		{
			printf("enter your password by first putting a space:");
			gets(check_pass);
			
			if(strcmp(check_pass,a[check_hash3].pass)==0)
			{
				checker=1;
				
				printf("\nyou have successfully logged in\n\n");
				return;
			}
		}
		
	}

	if(count_checker==n)
	{
		printf("\nthe username entered by you is wrong\n\n");
	}
	if(checker==0 && count_checker!=n)
	{
		printf("\nthe password entered by you is wrong\n\n");
	}
	
	
	
}
